-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
In languages like Pascal the semicolon was a used as a statement separator rather than a terminator, which was a always an annoyance and in my opinion also a design flaw. Languages that have come after have fixed that - but not for commas.
The missing comma causes issues when we put the comma separated list on multiple lines. It shows an extra line as modified in diffs and it makes it harder to reorder the items. The missing comma at the end is always a special case.
I would argue that the idea of a token-separated list rather than token-terminated list is a complicated pattern in the first place and the very definition includes a special case. There is always n-1 separators, unless there is 0 items, in which case there is also 0, not -1 separators. We as programmers know what it's like to write code that joins a string or writes out a comma-separated rather than terminated list. Either the last or the first item is always a special case.
I'm not saying you should or will put a comma at the end of your one-line arguments or tuples. I won't. We like to see a comma-separated list surrounded by parentheses. The trailing comma is ugly. But when it gets to multiple lines, it starts getting useful. As our statements get replaced by expressions, setters get replaced by constructor arguments, with tuples and generally C# becoming more expression-oriented,
commas are the new semicolon. Code like this is going to be more and more common:
return (
ResourceProvider.Instance.GetElectricityPrice(resources.electricity),
ResourceProvider.Instance.GetWaterPrice(resources.water),
ResourceProvider.Instance.GetGasPrice(resources.gas)
);
C# already allows trailing comma in enums, array and object initializers. My proposal is to also extend this to all comma-separated parenthesized lists in the language.
This would also introduce another way to disambiguate a one-tuple expression (though I would still probably prefer to name the tuple item instead). But it does seem to me like this is the fundamental reason that we can't parse that right now - if we're relying on comma separated lists, we can't be surprised that there's a special case for a 1-element tuple.
One disadvantage about is proposal is that it will introduce inconsistencies as some code will include them & some will not (but right now there's the inconsistency that there are already places in the language that allow a trailing comma).